let Jwt = require('jsonwebtoken');
// let WeChatServer = require('../server/weChat/weChatServer');
const passPath = [
    "/app/weChat/getToken",
    "/app/weChat/getWXConfig",
    "/app/topic/allList",
    "/app/user/nrList",
    "/app/image/getImage"
];
exports.verifyToken = function (req, res, next) {
    console.log(req.path);
    if (passPath.indexOf(req.path)>= 0) {
        next();
    } else {
        let token = req.headers['token'];
        if (token) {
            Jwt.verify(token, config.token_secret, function (err, result) {
                if (err)
                    return res.rej({code: 102, errorInfo: err});
                // if (result.expires_in > new Date().getTime()) {
                //     req.body.openid = result.openid;
                //     next();
                // } else if (result.invalid_in >= new Date().getTime()) {
                //     req.body.openid = result.openid;
                //     WeChatServer.requestToWeChat({
                //         serviceCode: "REFRESH_ACCESS_TOKEN",
                //         queryParams: {
                //             refresh_token: result.refresh_token
                //         }
                //     }).then(function () {
                //         next();
                //     }, res.rej);
                // } else {
                //     return res.rej({code: 103});
                // }
                req.body.openid = result.openid;
                next();
            })
        }else if(req.path==="/app/topic/list"){
            next();
        } else {
            return res.rej({code: 101});
        }
    }
};
